<?php
//
//	file: includes/aucp/notify_handler.php
//	begin: 2006-07-10
//	$Author: christopher $
//	$Revision: 29 $
//	$Date: 2008-07-19 15:59:35 +0000 (Sat, 19 Jul 2008) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	description: handler for notifications

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
}

class notify_parent
{
	var $handler, $users, $users_map, $data;
	var $read_data;

	// @constructor
	function __constructor($type='email', $notify_type='email')
	{
		// plugs
		$plug_ins = new plug_ins();
		$plug_ins->load('class_notify');
		unset($plug_ins);
		$this->plug_ins = &$config->plug_ins['class_notify'];

		$this->reset($type, $notify_type);
	}

	// backward compatible
	function notify_parent($type='email', $notify_type='email')
	{
		return $this->__constructor($type, $notify_type);
	}

	// @public clears out old data and, if necessary, creates a new handler
	function reset($type='', $notify_type='')
	{

		if ($type != '')
		{
			$this->set_handler($type);
		}

		$this->handler->reset($notify_type);
		$this->users = $this->users_map = array();

		return;
	}

	// @public adds an array of users
	function add_users ($users, $type='to')
	{
		if (!is_array($users))
		{
			// Plain string, multiple users
			if (strstr($users, ','))
			{
				$users = explode(',', $users);

			}
			// Plain string, single user
			else
			{
				$users = array($users);
			}
		}

		foreach($users as $user)
		{
			$this->users[intval($user)] = true;
			$this->users_map[$user] = $type;
		}

		return;
	}

	// @public gets all info into the system
	function prepare ($subject='', $message='', $subtitle='')
	{
		// Make the message ready for sending
		include($config->url('includes/functions_post'));
		$this->handler->set_message($message);
		$this->handler->set_subject($subject, $subtitle);

		return;
	}

	// just sends the data...nothing special there
	function send ()
	{
		$this->get_users();
		if (!$this->handler->send())
		{
			return false;
		}

		return;
	}

	// @public adds an array of headers
	// email only
	function add_headers ($headers)
	{
		if (!is_array($headers))
		{
			return false;
		}

		$this->handler->add_headers($headers);

		return;
	}

	// @public sets template file and variables for parsing email template
	// email only
	function template($tpl, $vars=array())
	{
		$this->handler->set_tpl($tpl);
		$this->handler->assign_vars($vars);

		return;
	}

	// @private sends email notification for pm recipients
	function pm_to_email()
	{
		global $user;

		// save user information
		$users_map = $this->users_map;
		$this->reset('email', 'email');

		//generate vars

		$this->template('advnot_pm', $vars);


		// add the users as recipients
		foreach($users_map as $user_id => $dummy)
		{
			if (isset($user->pool[$user_id]))
			{
				$this->handler->add_user($user->pool[$user_id]);
			}
		}

		$this->handler->send();

		return;


	}

	// @private sets the handler type
	function set_handler($type)
	{
		global $config;

		$handler = 'handler_' . $type;
		$file = $config->url('includes/aucp/' . $handler);


		if($this->handler)
		{
			unset($this->handler);
		}

		include($file);
		$this->handler = new $handler();

		return;
	}

	// @private gets the user information and adds it to the handler
	function get_users()
	{
		global $user;

		$this->data = array(
			'user_email',
			'user_email_type',
			'user_active',
			'user_lang',
			'user_id',
			'user_notify', 		// send any notification
			'user_notify_pm',	// notify on a new pm
			'user_notify_topic',	// notify on a new topic in a watched forum
			'user_notify_reply',	// notify on a reply to a watched topic
			'user_notify_email',	// receive emails from other users
		);

		if ( $this->plug_ins )
		{
			foreach ( $this->plug_ins as $plug => $dummy )
			{
				if ( method_exists($this->plug_ins[$plug], 'read') )
				{
					$this->plug_ins[$plug]->read($this->data);
				}
			}
		}

		$user->pool_fields = array_merge($user->pool_fields, $this->data);

		$user->read_pool($this->users);

		// only add found users
		foreach($this->users_map as $user_id => $type)
		{
			if (isset($user->pool[$user_id]))
			{
				$this->handler->add_user($user->pool[$user_id], $type);
			}
		}

		return;
	}
}
?>